<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods' }">商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-input placeholder="请输入内容" v-model="queryIfon.query" @clear="searchList" clearable class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="searchList"></el-button>
      </el-input>
      <el-button type="primary" style="margin-left: 30px; margin-bottom: 10px;" @click="goodsAdd">添加商品</el-button>
      <el-table :data="goodsList" border>
        <el-table-column label="#" width="50" prop="goods_id"></el-table-column>
        <el-table-column label="商品名称" width="700" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="110"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" prop="upd_time" width="160">
          <template slot-scope="scope">
            {{scope.row.upd_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" class="el-icon-edit" @click="modForm(scope.row.goods_id)" />
            <el-button type="danger" size="mini" class="el-icon-delete" @click="delForm(scope.row.goods_id)" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination :page-sizes="[1,10,50,100]" :page-size="queryIfon.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" :current-page="queryIfon.pagenum"
        @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>
    <!-- 编辑 -->
    <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
      <el-form :model="editParamsForm" :rules="editparamsRules" ref="editParamsFormRefs" label-width="100px">
        <el-form-item label="商品名称：" prop="goods_name">
          <el-input v-model="editParamsForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格：" prop="goods_price">
          <el-input v-model="editParamsForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量：" prop="goods_weight">
          <el-input v-model="editParamsForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品数量：" prop="goods_number">
          <el-input v-model="editParamsForm.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editParamsCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created () {
    this.getGoodsList()
  },
  data () {
    return {
      queryIfon: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      editDialogVisible: false,
      editParamsForm: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: ''
      },
      roleId: '',
      editparamsRules: {
        goods_name: [{
          required: true,
          message: '请填写商品名称',
          trigger: 'blur'
        }],
        goods_price: [{
          required: true,
          message: '请填写商品价格',
          trigger: 'blur'
        }],
        goods_weight: [{
          required: true,
          message: '请填写商品重量',
          trigger: 'blur'
        }],
        goods_number: [{
          required: true,
          message: '请填写商品数量',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    async getGoodsList () {
      const {
        data: res
      } = await this.$http.get('http://kb.dekunlu.top/api/private/v1/goods', {
        params: this.queryIfon
      })
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryIfon.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      this.queryIfon.pagenum = val
      this.getGoodsList()
    },
    // 搜索
    searchList () {
      this.getGoodsList()
    },
    // 编辑
    async modForm (id) {
      const {
        data: res
      } = await this.$http.get(`http://kb.dekunlu.top/api/private/v1/goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取信息失败')
      }
      this.editParamsForm = res.data
      this.editDialogVisible = true
    },
    editDialogClosed () {
      this.$refs.editParamsFormRefs.resetFields()
    },
    editParamsCate () {
      this.$refs.editParamsFormRefs.validate(async valid => {
        if (!valid) return this.$message.error('请填写完整用户信息')
        const {
          data: res
        } = await this.$http.put(
          'http://kb.dekunlu.top/api/private/v1/goods/' + this.editParamsForm.goods_id,
          this.editParamsForm
        )
        console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('编辑用户失败')
        }
        this.$message.success('编辑用户成功')
        this.editDialogVisible = false
        this.getGoodsList()
      })
    },
    // 删除
    async delForm (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      const {
        data: res
      } = await this.$http.delete(`http://kb.dekunlu.top/api/private/v1/goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除用户失败')
      this.$message.success('删除用户成功')
      this.getGoodsList()
    },
    goodsAdd () {
      this.$router.push('/goodsAdd')
    }
  }
}
</script>

<style lang="less" scoped>
  .el-input {
    width: 460px;
  }

  .el-pagination {
    margin-top: 10px;
  }
</style>
